<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/7
 * Time: 15:27
 */

namespace app\web\controller;
use app\common\controller\WebAdminBase;

use app\common\model\Blog as BlogModel;
use app\common\model\Category as CategoryModel;

class Admin extends WebAdminBase
{
    protected $_blog_model;
    protected $category_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->_blog_model = new BlogModel();
        $this->category_model = new CategoryModel();

        $category_level_list = $this->category_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);
    }

    /*
     * show all blogs
     * */
    public function index($cid = 0, $keyword = '', $page = 1)
    {
        //   $blog_list = new BlogModel();
        $blog_result = $this->_blog_model->showUser($cid, $keyword, $page);
        $this->assign('title', array(
            'title' => '首页'
        ));
        return $this->fetch('index', [
            'article_list' => $blog_result['article_list'],
            'category_list' => $blog_result['category_list'],
            'cid' => $blog_result['cid'],
            'keyword' => $blog_result['keyword'],
        ]);
    }

    /**
     * 添加文章
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $data['wechatname'] = json_encode(explode('|', $data['wechatname']));
            // 验证数据  上传图片返回服务器的地址
            $this->_blog_model->name('document');
            // 选取data数据上传到数据库
            $data['create_time'] = time();
            $re = $this->_blog_model->insert($data);
            if ($re) {
                $this->success('添加成功', 'web/admin/index');
            } else {
                $this->error('添加失败', 'web/admin/add');
            }
//            $res = $blog_model->allowField(true)->
        } else {
            $this->assign('id_name',$_SESSION['think']['admin_name']);
            $this->assign('title', array(
                'title' => '添加文案',
            ));
            return $this->fetch('add');
        }


    }

    public function update()
    {
        $data = request()->param('id');
        //读取表数据
        $Bloglist = $this->_blog_model->showOneBlog($data);
        if (json_decode($Bloglist[0]['wechatname'])) {
            $Bloglist[0]['wechatname'] = implode('|', json_decode($Bloglist[0]['wechatname']));
        }
        if (!$Bloglist) {
            $this->error('信息不存在或已被删除', 'web/admin/index');
        } else {
            $this->assign('Bloglist', $Bloglist[0]);
        }
        $this->assign('title', array(
            'title' => '修改文案'
        ));
        return $this->fetch('update');
    }

    public function save()
    {
        $data = request()->param();
        $data['wechatname'] = json_encode(explode('|', $data['wechatname']));
        $data['modification_time'] = time();
        $re = $this->_blog_model->name('document')->where('id', $data['id'])->update($data);
        if ($re) {
            $this->success('修改成功', 'web/Admin/index');
        } else {
            $this->error('修改失败', 'web/Admin/update');
        }

    }

    public function delete()
    {
        $data = request()->param('id');
        $this->_blog_model->name('document');
        $delete = $this->_blog_model->where('id', $data)->delete();
        if ($delete) {
            $this->success('删除成功', 'web/Admin/index');
        } else {
            $this->error('删除失败', 'web/Admin/index');
        }
    }
}